package com.lwj.springbootvue.service.impl;

import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwj.springbootvue.constants.HttpStatus;
import com.lwj.springbootvue.exception.ServiceException;
import com.lwj.springbootvue.mapper.UserMapper;
import com.lwj.springbootvue.model.User;
import com.lwj.springbootvue.model.vo.StudentVo;
import com.lwj.springbootvue.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lwj.springbootvue.mapper.TeacherMapper;
import com.lwj.springbootvue.model.Teacher;
import com.lwj.springbootvue.service.ITeacherService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 教师Service业务层处理
 * 
 * @author king
 * @date 2024-12-25
 */
@Service
public class TeacherServiceImpl implements ITeacherService 
{
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询教师
     * 
     * @param tid 教师主键
     * @return 教师
     */
    @Override
    public Teacher selectTeacherByTid(Long tid)
    {
        return teacherMapper.selectTeacherByTid(tid);
    }

    /**
     * 查询教师列表
     * 
     * @param teacher 教师
     * @return 教师
     */
    @Override
    public PageInfo<Teacher> selectTeacherList(Teacher teacher)
    {
        // 开启分页
        PageHelper.startPage(Math.toIntExact(teacher.getPageNum()), Math.toIntExact(teacher.getPageSize()));// 开启分页
        // pageHelp 会自动分页查询
        List<Teacher> teacherList = teacherMapper.selectTeacherList(teacher);
        // 封装pageInfo 对象
        PageInfo<Teacher> pageInfo=new PageInfo<Teacher>(teacherList);
        return pageInfo;
    }

    /**
     * 新增教师
     * 
     * @param teacher 教师
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher)
    {
        teacher.setCreateTime(LocalDateTimeUtil.now());
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改教师
     * 
     * @param teacher 教师
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher)
    {
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除教师
     * 
     * @param tids 需要删除的教师主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTeacherByTids(Long[] tids)
    {
        for(Long tid:tids){
            try{
                User user = userMapper.selectUserByUtid(tid);
                if(ObjectUtil.isNotNull(user)){
                    throw new ServiceException(HttpStatus.CONFLICT,"删除失败，对应的管理员账户未解除绑定，请联系高级管理员！");
                }
            }catch (ServiceException e){
                System.out.println("删除教师失败，账号未解除绑定！");
            }
        }
        return teacherMapper.deleteTeacherByTids(tids);
    }

    /**
     * 删除教师信息
     * 
     * @param tid 教师主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTeacherByTid(Long tid)
    {
        User user = userMapper.selectUserByUtid(tid);
        if(ObjectUtil.isNotNull(user)){
            throw new ServiceException(HttpStatus.CONFLICT,"删除失败，对应的管理员账户未解除绑定，请联系高级管理员！");
        }
        return teacherMapper.deleteTeacherByTid(tid);
    }
}
